<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CSS选择器测试工具</title>
    <style>
        html,
        body {
            font-size: 12px;
        }

        #test {
            width: 200px;
            display: flex;
            flex-direction: row;
            flex-wrap: wrap;
        }

        .div_test {
            width: 30px;
            height: 30px;
            background-color: black;
            color: white;
            margin: 5px;
            border-radius: 15px;
            vertical-align: -13px;
        }

        .selected {
            color: red;
            border: 1px solid red;
            font-size: 1.2rem;
        }

        .div-test {
            width: 300px;
            border: 1px solid #ccc;
            display: -webkit-box;
            -webkit-box-orient: vertical;
            -webkit-line-clamp: 3;
            overflow: hidden;
        }
    </style>
</head>

<body>
    <article id="css-selector">
        <input type="text" name="css-value" id="css-value" placeholder="请输入选择器文本"><button id="find">查询</button>
    </article>
    <hr>
    <article id="css-content">
        <a href="#">我是一个超链接</a><br>
        <ul>
            <li>java</li>
            <li>C++</li>
            <li>python</li>
        </ul>
        <div><a href="#">我也是一个超链接</a></div>
        <div class="div1"></div>
        <ul>
            <li>java</li>
            <li>C++</li>
            <li>python</li>
        </ul>
        <ul>
            <li>java</li>
            <li>C++</li>
            <li>python</li>
        </ul>
        <div>
            <div class="div2_level1">我是一个有两个class值的div</div>
            <ul>
                <li>java</li>
                <li>C++</li>
                <li>python</li>
            </ul>
        </div>
        <div></div>
        <p></p>
        <button type="button">一般按钮</button>
        <button type="submit">提交按钮</button>
        <div style="border:1px solid #ccc">
            <div style="float:left">第一个左浮动</div>
            <div style="float:left">第二个左浮动</div>
            <div class="clear"></div>
        </div>
        <ol>
            <li>独一份</li>
        </ol>
        <section id="test">
            <nav class="div_test"><span style="margin:8px;line-height: 30px;">1</span></nav>
            <div class="div_test"><span style="margin:8px;line-height: 30px;">2</span></div>
            <div class="div_test"><span style="margin:8px;line-height: 30px;">3</span></div>
            <div class="div_test"><span style="margin:8px;line-height: 30px;">4</span></div>
            <div class="div_test"><span style="margin:8px;line-height: 30px;">5</span></div>
            <div class="div_test"><span style="margin:8px;line-height: 30px;">6</span></div>
            <div class="div_test"><span style="margin:8px;line-height: 30px;">7</span></div>
            <div class="div_test"><span style="margin:8px;line-height: 30px;">8</span></div>
            <div class="div_test"><span style="margin:8px;line-height: 30px;">9</span></div>
            <div class="div_test"><span style="margin:6px;line-height: 30px;">10</span></div>
            <div class="div_test"><span style="margin:6px;line-height: 30px;">11</span></div>
            <div class="div_test"><span style="margin:6px;line-height: 30px;">12</span></div>
            <div class="div_test"><span style="margin:6px;line-height: 30px;">13</span></div>
            <div class="div_test"><span style="margin:6px;line-height: 30px;">14</span></div>
            <div class="div_test"><span style="margin:6px;line-height: 30px;">15</span></div>
            <div class="div_test"><span style="margin:6px;line-height: 30px;">16</span></div>
            <div class="div_test"><span style="margin:6px;line-height: 30px;">17</span></div>
            <div class="div_test"><span style="margin:6px;line-height: 30px;">18</span></div>
            <div class="div_test"><span style="margin:6px;line-height: 30px;">19</span></div>
            <div class="div_test"><span style="margin:6px;line-height: 30px;">20</span></div>
        </section>
        <input list="cars" />
        <datalist id="cars">
            <option value="BMW">
            <option value="Ford">
            <option value="Volvo">
        </datalist>
    </article>
    <article>
        <div class="div-test">
            行框（line
            box），同行内框类似的概念，行框是指本行的一个虚拟的矩形框，也是浏览器渲染模式中的一个概念。行框高度等于本行内所有元素中行内框最大的值（以行高值最大的行内框为基准，其他行内框采用自己的对齐方式向基准对齐，最终计算行框的高度）。
        </div>
    </article>
</body>

</html>
<script>
    //单击事件监听器
    document.querySelector("#find").addEventListener('click', event => {
        //单击事件处理程序
        //恢复操作
        let arrays = document.getElementById("css-content").querySelectorAll("*");  //获取css-content中的所有节点
        arrays.forEach(node => {
            node.classList.remove("selected");  //删除掉可能有class
        });
        let cssValue = document.getElementById("css-value").value;    //获取文本框中的输入值，这个输入值应该是一个CSS选择器文本
        let results = document.getElementById("css-content").querySelectorAll(cssValue);    //获取当前节点中所有符合要求的节点
        results.forEach(node => {
            node.classList.add("selected");     //给选中的节点添加指定的class
        });
    }, false);
</script>